/*
 * EditarProjeto.java
 *
 * Created on 7 de Julho de 2008, 19:49
 */

package gerenciadorprojetos.forms;

import gerenciadorprojetos.exception.StatusServiceException;
import gerenciadorprojetos.services.ProjetoService;
import gerenciadorprojetos.services.StatusService;
import gerenciadorprojetos.services.UsuarioService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.ListIterator;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.jdesktop.application.Action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author  eduardo
 */
public class EditarProjeto extends javax.swing.JDialog {

    /** Creates new form EditarProjeto */
    public EditarProjeto(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelID = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        jLabelDescricao = new javax.swing.JLabel();
        jLabelDtaIni = new javax.swing.JLabel();
        jLabelDtaPrevInici = new javax.swing.JLabel();
        jLabelDtaFim = new javax.swing.JLabel();
        jLabelDtaPrevFim = new javax.swing.JLabel();
        jLabelExecutor = new javax.swing.JLabel();
        jLabelStatus = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jTextFieldNome = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDescricao = new javax.swing.JTextArea();
        jFormattedTextFieldDtaIni = new javax.swing.JFormattedTextField();
        jFormattedTextFieldPrevIni = new javax.swing.JFormattedTextField();
        jFormattedTextFieldDtaFim = new javax.swing.JFormattedTextField();
        jFormattedTextFieldPrevFim = new javax.swing.JFormattedTextField();
        jComboBoxExecutor = new javax.swing.JComboBox();
        jComboBoxStatus = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jButtonEditar = new javax.swing.JButton();
        jButtonStatus = new javax.swing.JButton();
        jButtonReplanejar = new javax.swing.JButton();
        jButtonRedirecionar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jButtonSair = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getResourceMap(EditarProjeto.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, resourceMap.getColor("jPanel1.border.highlightColor"), null)); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabelID.setText(resourceMap.getString("jLabelID.text")); // NOI18N
        jLabelID.setName("jLabelID"); // NOI18N

        jLabelNome.setText(resourceMap.getString("jLabelNome.text")); // NOI18N
        jLabelNome.setName("jLabelNome"); // NOI18N

        jLabelDescricao.setText(resourceMap.getString("jLabelDescricao.text")); // NOI18N
        jLabelDescricao.setName("jLabelDescricao"); // NOI18N

        jLabelDtaIni.setText(resourceMap.getString("jLabelDtaIni.text")); // NOI18N
        jLabelDtaIni.setName("jLabelDtaIni"); // NOI18N

        jLabelDtaPrevInici.setText(resourceMap.getString("jLabelDtaPrevInici.text")); // NOI18N
        jLabelDtaPrevInici.setName("jLabelDtaPrevInici"); // NOI18N

        jLabelDtaFim.setText(resourceMap.getString("jLabelDtaFim.text")); // NOI18N
        jLabelDtaFim.setName("jLabelDtaFim"); // NOI18N

        jLabelDtaPrevFim.setText(resourceMap.getString("jLabelDtaPrevFim.text")); // NOI18N
        jLabelDtaPrevFim.setName("jLabelDtaPrevFim"); // NOI18N

        jLabelExecutor.setText(resourceMap.getString("jLabelExecutor.text")); // NOI18N
        jLabelExecutor.setName("jLabelExecutor"); // NOI18N

        jLabelStatus.setText(resourceMap.getString("jLabelStatus.text")); // NOI18N
        jLabelStatus.setName("jLabelStatus"); // NOI18N

        jTextFieldId.setEditable(false);
        jTextFieldId.setText(resourceMap.getString("jTextFieldId.text")); // NOI18N
        jTextFieldId.setName("jTextFieldId"); // NOI18N

        jTextFieldNome.setText(resourceMap.getString("jTextFieldNome.text")); // NOI18N
        jTextFieldNome.setName("jTextFieldNome"); // NOI18N

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextAreaDescricao.setColumns(20);
        jTextAreaDescricao.setRows(5);
        jTextAreaDescricao.setName("jTextAreaDescricao"); // NOI18N
        jScrollPane1.setViewportView(jTextAreaDescricao);

        jFormattedTextFieldDtaIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yy HH:mm"))));
        jFormattedTextFieldDtaIni.setName("jFormattedTextFieldDtaIni"); // NOI18N

        jFormattedTextFieldPrevIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yy HH:mm"))));
        jFormattedTextFieldPrevIni.setName("jFormattedTextFieldPrevIni"); // NOI18N

        jFormattedTextFieldDtaFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yy HH:mm"))));
        jFormattedTextFieldDtaFim.setName("jFormattedTextFieldDtaFim"); // NOI18N

        jFormattedTextFieldPrevFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yy HH:mm"))));
        jFormattedTextFieldPrevFim.setName("jFormattedTextFieldPrevFim"); // NOI18N

        jComboBoxExecutor.setName("jComboBoxExecutor"); // NOI18N

        jComboBoxStatus.setName("jComboBoxStatus"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDtaFim)
                    .addComponent(jLabelDtaIni)
                    .addComponent(jLabelExecutor)
                    .addComponent(jLabelStatus)
                    .addComponent(jLabelDescricao)
                    .addComponent(jLabelNome)
                    .addComponent(jLabelID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 853, Short.MAX_VALUE)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 853, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 853, Short.MAX_VALUE)
                    .addComponent(jComboBoxStatus, 0, 853, Short.MAX_VALUE)
                    .addComponent(jComboBoxExecutor, 0, 853, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jFormattedTextFieldDtaFim)
                            .addComponent(jFormattedTextFieldDtaIni, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabelDtaPrevFim)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jFormattedTextFieldPrevFim, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabelDtaPrevInici)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jFormattedTextFieldPrevIni, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jFormattedTextFieldDtaFim, jFormattedTextFieldDtaIni, jFormattedTextFieldPrevFim, jFormattedTextFieldPrevIni});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelID)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNome)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDescricao)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDtaIni)
                    .addComponent(jLabelDtaPrevInici)
                    .addComponent(jFormattedTextFieldDtaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldPrevIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDtaFim)
                    .addComponent(jLabelDtaPrevFim)
                    .addComponent(jFormattedTextFieldPrevFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldDtaFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelExecutor)
                    .addComponent(jComboBoxExecutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelStatus)
                    .addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getActionMap(EditarProjeto.class, this);
        jButtonEditar.setAction(actionMap.get("Editar")); // NOI18N
        jButtonEditar.setName("jButtonEditar"); // NOI18N

        jButtonStatus.setAction(actionMap.get("AlterarStatus")); // NOI18N
        jButtonStatus.setName("jButtonStatus"); // NOI18N

        jButtonReplanejar.setAction(actionMap.get("Replanejar")); // NOI18N
        jButtonReplanejar.setName("jButtonReplanejar"); // NOI18N

        jButtonRedirecionar.setAction(actionMap.get("Redirecionar")); // NOI18N
        jButtonRedirecionar.setName("jButtonRedirecionar"); // NOI18N

        jButtonCancelar.setAction(actionMap.get("Cancelar")); // NOI18N
        jButtonCancelar.setName("jButtonCancelar"); // NOI18N

        jButtonSair.setAction(actionMap.get("Sair")); // NOI18N
        jButtonSair.setName("jButtonSair"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(jButtonEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonReplanejar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonRedirecionar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSair, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(93, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonCancelar, jButtonEditar, jButtonRedirecionar, jButtonReplanejar, jButtonSair, jButtonStatus});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jButtonEditar))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jButtonStatus))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jButtonReplanejar))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jButtonRedirecionar))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jButtonCancelar))
                    .addComponent(jButtonSair))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButtonCancelar, jButtonEditar, jButtonRedirecionar, jButtonReplanejar, jButtonSair, jButtonStatus});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void Editar() {
        if ("SALVAR ...".equals(jButtonEditar.getText())) {
           if (! pS.atualizarProjeto(jTextFieldId.getText(), jTextFieldNome.getText(), jTextAreaDescricao.getText(), jFormattedTextFieldPrevIni.getText(), jFormattedTextFieldPrevFim.getText(), jFormattedTextFieldDtaIni.getText(), jFormattedTextFieldDtaFim.getText(), (String)jComboBoxExecutor.getSelectedItem(), (String)jComboBoxStatus.getSelectedItem())) {
               JOptionPane.showMessageDialog(this, "Ocorreu um erro ao atualizar o projeto, se o erro persistir favor contacte o analista reponsavel pelo sistema !");
           }
           this.manipulaStatusBotoes();
            
           jTextFieldNome.setEditable(false);
           jTextAreaDescricao.setEditable(false);
        } else {
           jButtonCancelar.setEnabled(false);
           jButtonEditar.setEnabled(true);
           jButtonEditar.setText("SALVAR ...");
           jButtonRedirecionar.setEnabled(false);
           jButtonReplanejar.setEnabled(false);
           jButtonStatus.setEnabled(false); 
           
           jTextFieldNome.setEditable(true);
           jTextAreaDescricao.setEditable(true);
        }
    }

    @Action
    public void AlterarStatus() {
        if ("PLANEJAR ...".equals(jButtonStatus.getText())) {
            jButtonCancelar.setEnabled(false);
            jButtonEditar.setEnabled(false);
            jButtonRedirecionar.setEnabled(false);
            jButtonReplanejar.setEnabled(false);
            jButtonStatus.setEnabled(true);
            jButtonStatus.setText("SALVAR ...");
            
            SimpleDateFormat d = new SimpleDateFormat("dd/MM/yyy HH:mm");
            Date date = new Date();
            jFormattedTextFieldPrevIni.setText(d.format(date));
            jFormattedTextFieldPrevIni.setEditable(true);
            
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            gc.add(GregorianCalendar.DAY_OF_WEEK, 30);
            jFormattedTextFieldPrevFim.setText(d.format(gc.getTime()));
            jFormattedTextFieldPrevFim.setEditable(true);
        } else if (("EXECUTAR ...").equals(jButtonStatus.getText())) {
            jButtonCancelar.setEnabled(false);
            jButtonEditar.setEnabled(false);
            jButtonRedirecionar.setEnabled(false);
            jButtonReplanejar.setEnabled(false);
            jButtonStatus.setEnabled(true);
            jButtonStatus.setText("SALVAR ...");
            
            SimpleDateFormat d = new SimpleDateFormat("dd/MM/yyy HH:mm");
            Date date = new Date();
            jFormattedTextFieldDtaIni.setText(d.format(date));
            jFormattedTextFieldDtaIni.setEditable(true);
        } else if (("ENCERRAR ...").equals(jButtonStatus.getText())) {
            jButtonCancelar.setEnabled(false);
            jButtonEditar.setEnabled(false);
            jButtonRedirecionar.setEnabled(false);
            jButtonReplanejar.setEnabled(false);
            jButtonStatus.setEnabled(true);
            jButtonStatus.setText("SALVAR ...");
            
            SimpleDateFormat d = new SimpleDateFormat("dd/MM/yyy HH:mm");
            Date date = new Date();
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            jFormattedTextFieldDtaFim.setText(d.format(gc.getTime()));
            jFormattedTextFieldDtaFim.setEditable(true);
        } else if (("SALVAR ...").equals(jButtonStatus.getText())) {
                       
           this.alteraBotaoStatus();
            
           if (! pS.atualizarProjeto(jTextFieldId.getText(), jTextFieldNome.getText(), jTextAreaDescricao.getText(), jFormattedTextFieldPrevIni.getText(), jFormattedTextFieldPrevFim.getText(), jFormattedTextFieldDtaIni.getText(), jFormattedTextFieldDtaFim.getText(), (String)jComboBoxExecutor.getSelectedItem(), (String)jComboBoxStatus.getSelectedItem())) {
               JOptionPane.showMessageDialog(this, "Ocorreu um erro ao atualizar o projeto, se o erro persistir favor contacte o analista reponsavel pelo sistema !");
           }
           this.manipulaStatusBotoes();
           
           jFormattedTextFieldDtaIni.setEditable(false);
           jFormattedTextFieldDtaFim.setEditable(false);
           jFormattedTextFieldPrevIni.setEditable(false);
           jFormattedTextFieldPrevFim.setEditable(false);
        } else {
           JOptionPane.showMessageDialog(this, "Ocorreu um bug no sistema, favor informar ao analista responsável !");
            this.manipulaStatusBotoes(); 
        }
    }
    
    public void alteraBotaoStatus() {
         if ("REDIRECIONADO".equals(jComboBoxStatus.getSelectedItem())) {
             jComboBoxStatus.setSelectedItem("PLANEJADO");
         } else if ("NOVO".equals(jComboBoxStatus.getSelectedItem())) {
             jComboBoxStatus.setSelectedItem("PLANEJADO");
         } else if ("PLANEJADO".equals(jComboBoxStatus.getSelectedItem())) {
             jComboBoxStatus.setSelectedItem("EM ANDAMENTO");
         } else if ("EM ANDAMENTO".equals(jComboBoxStatus.getSelectedItem())) {
             jComboBoxStatus.setSelectedItem("ENCERRADO");
         }
    }

    @Action
    public void Replanejar() {
        if ("SALVAR ...".equals(jButtonReplanejar.getText())) {
            if (! pS.replanejaProjeto(jTextFieldId.getText(), jFormattedTextFieldPrevIni.getText(), jFormattedTextFieldPrevFim.getText())) {
                JOptionPane.showMessageDialog(this, "Ocorreu um erro ao replanejar o projeto, se o erro persistir favor contacte o analista reponsavel pelo sistema !");
            }
            
            this.manipulaStatusBotoes();
            jFormattedTextFieldPrevIni.setEditable(false);
            jFormattedTextFieldPrevFim.setEditable(false);
        } else if ("REPLANEJAR ...".equals(jButtonReplanejar.getText())) {
            jButtonCancelar.setEnabled(false);
            jButtonEditar.setEnabled(false);
            jButtonRedirecionar.setEnabled(false);
            jButtonReplanejar.setEnabled(true);
            jButtonReplanejar.setText("SALVAR ...");
            jButtonStatus.setEnabled(false);
                        
            jFormattedTextFieldPrevIni.setEditable(true);
            jFormattedTextFieldPrevFim.setEditable(true);
        } else {
            JOptionPane.showMessageDialog(this, "Ocorreu um bug no sistema, favor informar ao analista responsável !");
            this.manipulaStatusBotoes(); 
        }
    }

    @Action
    public void Redirecionar() {
        if ("SALVAR ...".equals(jButtonRedirecionar.getText())) {
            if (! pS.redirecionarProjeto(jTextFieldId.getText(), (String)jComboBoxExecutor.getSelectedItem())) {
                JOptionPane.showMessageDialog(this, "Ocorreu um erro ao redirecionar o projeto, se o erro persistir favor contacte o analista reponsavel pelo sistema !");
            }
            
            this.manipulaStatusBotoes();
            jComboBoxExecutor.setEnabled(false);
            jComboBoxStatus.setSelectedItem("REDIRECIONADO");
        } else if ("REDIRECIONAR ...".equals(jButtonRedirecionar.getText())) {
            jButtonCancelar.setEnabled(false);
            jButtonEditar.setEnabled(false);
            jButtonRedirecionar.setEnabled(true);
            jButtonRedirecionar.setText("SALVAR ...");
            jButtonReplanejar.setEnabled(false);
            jButtonStatus.setEnabled(false);
                        
            jComboBoxExecutor.setEnabled(true);
        } else {
            JOptionPane.showMessageDialog(this, "Ocorreu um bug no sistema, favor informar ao analista responsável !");
            this.manipulaStatusBotoes(); 
        }
    }

    @Action
    public void Cancelar() {
        logger.debug("*** Cancelando Projeto");
        if (JOptionPane.showConfirmDialog(this, "Voce confirma o cancelamento deste projeto ?", "Cancelando o projeto !", JOptionPane.YES_NO_OPTION) > 0) {
            logger.debug("Foi rejeitado o cancelamento do projeto !");
        } else {
            logger.debug("Foi solicitado o cancelamento do projeto !");
            
            if (! pS.atualizarProjeto(jTextFieldId.getText(), jTextFieldNome.getText(), jTextAreaDescricao.getText(), jFormattedTextFieldPrevIni.getText(), jFormattedTextFieldPrevFim.getText(), jFormattedTextFieldDtaIni.getText(), jFormattedTextFieldDtaFim.getText(), (String)jComboBoxExecutor.getSelectedItem(), "CANCELADO")) {
               JOptionPane.showMessageDialog(this, "Ocorreu um erro ao atualizar o projeto, se o erro persistir favor contacte o analista reponsavel pelo sistema !");
            } else {
                jComboBoxStatus.setSelectedItem("CANCELADO");
            }
            
            this.manipulaStatusBotoes();
        }
    }

    @Action
    public void Sair() {
        this.setVisible(false);
    }
    
    @SuppressWarnings("empty-statement")
    public void inicializaFormulario(String projeto) {
        context = new ClassPathXmlApplicationContext("spring-servlet.xml");
        
        UsuarioService uS = (UsuarioService)context.getBean("usuarioService");
        jComboBoxExecutor.removeAllItems();
        jComboBoxExecutor.addItem("");
        
        List<String> l = uS.getUsuarios();
        ListIterator<String> it = l.listIterator();
        do
        {
            jComboBoxExecutor.addItem(it.next());
        } while(it.hasNext());
        
        StatusService sS = (StatusService)context.getBean("statusService");
        jComboBoxStatus.removeAllItems();
        
        l = sS.getAllStatus();
        
        for(it = l.listIterator(); it.hasNext(); jComboBoxStatus.addItem(it.next()));
        
        pS = (ProjetoService)context.getBean("projetoService");
        
        String s = pS.getProjeto(projeto);
        
        StringTokenizer t = new StringTokenizer(s, ";");
        
        jTextFieldId.setText(t.nextToken());
        jTextFieldNome.setText(t.nextToken());
        jTextAreaDescricao.setText(t.nextToken());
        jFormattedTextFieldDtaIni.setText(t.nextToken());
        jFormattedTextFieldPrevIni.setText(t.nextToken());
        jFormattedTextFieldDtaFim.setText(t.nextToken());
        jFormattedTextFieldPrevFim.setText(t.nextToken());
        
        String aux = t.nextToken();
        System.out.println("Token de saida: "+aux);
        if (! "".equals(aux)) {
            jComboBoxExecutor.setSelectedItem(aux);
        } else {
            jComboBoxExecutor.setSelectedItem("");
        }
        
        t.nextToken();
        jComboBoxStatus.setSelectedItem(t.nextToken());

        jTextAreaDescricao.setEditable(false);
        jTextFieldId.setEditable(false);
        jTextFieldNome.setEditable(false);
        jFormattedTextFieldDtaFim.setEditable(false);
        jFormattedTextFieldDtaIni.setEditable(false);
        jFormattedTextFieldPrevFim.setEditable(false);
        jFormattedTextFieldPrevIni.setEditable(false);
        jComboBoxExecutor.setEnabled(false);
        jComboBoxStatus.setEnabled(false);
        
        this.manipulaStatusBotoes();
    }
    
    private int getIdStatus() {
        
        try {
            String aux = (String)jComboBoxStatus.getSelectedItem();
            logger.debug("STATUS ATUAL DO PROJETO: "+aux);
            
            StatusService sS = (StatusService)context.getBean("statusService");
            return sS.getIdStatus(aux);
        } catch (StatusServiceException ex) {
            logger.debug("*** Ocorreu um erro ao parser o id !");
            return -1;
        }
        
    }
    
    private void manipulaStatusBotoes () {
        int sA = this.getIdStatus();
        
        switch(sA) {
            case -1:
                logger.debug("*** Ocorreu um erro ao obter o status !");
                jButtonCancelar.setEnabled(false);
                jButtonEditar.setEnabled(false);
                jButtonRedirecionar.setEnabled(false);
                jButtonReplanejar.setEnabled(false);
                jButtonStatus.setEnabled(false);
                break;
            case 0:
                logger.debug("*** NOVO");
                jButtonCancelar.setEnabled(true);
                jButtonCancelar.setText("CANCELAR ...");
                jButtonEditar.setEnabled(true);
                jButtonEditar.setText("EDITAR ...");
                jButtonRedirecionar.setEnabled(true);
                jButtonRedirecionar.setText("REDIRECIONAR ...");
                jButtonReplanejar.setEnabled(false);
                jButtonReplanejar.setText("REPLANEJAR ...");
                jButtonStatus.setEnabled(true);
                jButtonStatus.setText("PLANEJAR ...");
                break;
            case 1:
                logger.debug("*** PLANEJADO");
                jButtonCancelar.setEnabled(true);
                jButtonCancelar.setText("CANCELAR ...");
                jButtonEditar.setEnabled(true);
                jButtonEditar.setText("EDITAR ...");
                jButtonRedirecionar.setEnabled(true);
                jButtonRedirecionar.setText("REDIRECIONAR ...");
                jButtonReplanejar.setEnabled(true);
                jButtonReplanejar.setText("REPLANEJAR ...");
                jButtonStatus.setEnabled(true);
                jButtonStatus.setText("EXECUTAR ...");
                break;
            case 2:
                logger.debug("*** EM ANDAMENTO");
                jButtonCancelar.setEnabled(true);
                jButtonCancelar.setText("CANCELAR ...");
                jButtonEditar.setEnabled(true);
                jButtonEditar.setText("EDITAR ...");
                jButtonRedirecionar.setEnabled(true);
                jButtonRedirecionar.setText("REDIRECIONAR ...");
                jButtonReplanejar.setEnabled(true);
                jButtonReplanejar.setText("REPLANEJAR ...");
                jButtonStatus.setEnabled(true);
                jButtonStatus.setText("ENCERRAR ...");
                break;
            case 3:
                logger.debug("*** ENCERRADO");
                jButtonCancelar.setEnabled(false);
                jButtonCancelar.setText("CANCELAR ...");
                jButtonEditar.setEnabled(false);
                jButtonEditar.setText("EDITAR ...");
                jButtonRedirecionar.setEnabled(false);
                jButtonRedirecionar.setText("REDIRECIONAR ...");
                jButtonReplanejar.setEnabled(false);
                jButtonReplanejar.setText("REPLANEJAR ...");
                jButtonStatus.setEnabled(false);
                jButtonStatus.setText("STATUS");
                break;
            case 4:
                logger.debug("*** ENCERRADO");
                jButtonCancelar.setEnabled(false);
                jButtonCancelar.setText("CANCELAR ...");
                jButtonEditar.setEnabled(false);
                jButtonEditar.setText("EDITAR ...");
                jButtonRedirecionar.setEnabled(false);
                jButtonRedirecionar.setText("REDIRECIONAR ...");
                jButtonReplanejar.setEnabled(false);
                jButtonReplanejar.setText("REPLANEJAR ...");
                jButtonStatus.setEnabled(false);
                jButtonStatus.setText("STATUS");
                break;
            case 5:
                logger.debug("*** REDIRECIONADO");
                jButtonCancelar.setEnabled(true);
                jButtonCancelar.setText("CANCELAR ...");
                jButtonEditar.setEnabled(true);
                jButtonEditar.setText("EDITAR ...");
                jButtonRedirecionar.setEnabled(true);
                jButtonRedirecionar.setText("REDIRECIONAR ...");
                jButtonReplanejar.setEnabled(false);
                jButtonReplanejar.setText("REPLANEJAR ...");
                jButtonStatus.setEnabled(true);
                jButtonStatus.setText("PLANEJAR ...");
                break;
            default:
                logger.debug("*** OUTROS IDS - "+sA);
                jButtonCancelar.setEnabled(false);
                jButtonEditar.setEnabled(false);
                jButtonRedirecionar.setEnabled(false);
                jButtonReplanejar.setEnabled(false);
                jButtonStatus.setEnabled(false);
                break;
        }
        
        //Editar Buttun
        
        //Status Buttun
        
        //Replanejamento Buttun
        
        //Cancelar Buttun
        
        //Redirecionar Buttun
        
        //Sair Buttun
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonEditar;
    private javax.swing.JButton jButtonRedirecionar;
    private javax.swing.JButton jButtonReplanejar;
    private javax.swing.JButton jButtonSair;
    private javax.swing.JButton jButtonStatus;
    private javax.swing.JComboBox jComboBoxExecutor;
    private javax.swing.JComboBox jComboBoxStatus;
    private javax.swing.JFormattedTextField jFormattedTextFieldDtaFim;
    private javax.swing.JFormattedTextField jFormattedTextFieldDtaIni;
    private javax.swing.JFormattedTextField jFormattedTextFieldPrevFim;
    private javax.swing.JFormattedTextField jFormattedTextFieldPrevIni;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelDtaFim;
    private javax.swing.JLabel jLabelDtaIni;
    private javax.swing.JLabel jLabelDtaPrevFim;
    private javax.swing.JLabel jLabelDtaPrevInici;
    private javax.swing.JLabel jLabelExecutor;
    private javax.swing.JLabel jLabelID;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaDescricao;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldNome;
    // End of variables declaration//GEN-END:variables

    ApplicationContext context;
    ProjetoService pS;
    Logger logger = Logger.getLogger(EditarProjeto.class);
}